<?php

/**
 * Description: 获取试卷题目及练习报告
 * User: lizengcai
 * Date: 16/12/6
 * Time: 11:00
 */

require __DIR__ . '/includes/utils.php';
class HistoryExerciseDetail implements iController {
    

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $uddb;

        if (empty($queryArgs['exercise_id']))
            return genOutput(1000);

        $sql = 'SELECT * FROM `institution_user_paper` WHERE `id`=:exercise_id';
        $exercise = $uddb->getRow($sql, array(':exercise_id' => $queryArgs['exercise_id']));

        if (empty($exercise)) {
            return genOutput(1000);
        }

        //是否为只看错题
        if (!empty($queryArgs['only_error']) && $queryArgs['only_error'] == 'true')
            $onlyError = true;
        else
            $onlyError = false;

            $answers = array();
            $qids = array();
            if (($questions = json_decode($exercise->question_info)) && is_array($questions)) {
                foreach ($questions as $q) {
                    if ($onlyError && $q->is_right)
                        continue;

                    $answers[] = array(
                        'id'        => intval($q->id),
                        'is_right'  => $q->is_right ? true : false,
                        'answer'    => $q->answer,
                        'duration'  => intval($q->duration),
                    );
                    $qids[] = $q->id;
                }
            } else {
                return genOutput(1001);
            }

            if ($exercise->status == 0)
                $status = 'fresh';
            elseif ($exercise->status == 1)
                $status = 'done';
            else
                $status = 'undone';

            //问题
            $questionsData            = InstitutionUtils::getQuestionsByInstitutionIds($qids);
            return genOutput(1, array(
                'exercise_name'         => $exercise->paper_title,
                'status'                => $status,
                'duration'              => 900,
                'start_from'            => intval($exercise->duration),
                'questions'             => $questionsData,
                'answers'               => $answers,
                'total_count'           =>$exercise->total_count,
                'right_count'           =>$exercise->right_count,
                'evaluate'              =>$exercise->evaluate,
                'defeat'                => $status === 'done' && $exercise->paper_type == '1'
                    ? $this->getInstitutionRank($exercise->paper_id, $exercise->evaluate) : 0.0,
                'score'                 => 0,
                'scores'                => array()
            ));


    }

    /**
     * 获取事业单位排名信息
     * @param int $paperId 事业单位id
     * @param float $score 待比较分数
     * @return float
     */
    private function getInstitutionRank($paperId, $score) {
        global $uddb;

        $rankScore = intval($score);

        //通过缓存获取模考信息
        $evaluateArr = InstitutionUtils::getInstitutionEvaluate($paperId);
        //计算击败了多少
        $total = $lower = 0;
        foreach ($evaluateArr as $score => $persons) {
            $total += $persons;
            if ($score < $rankScore) {
                $lower += $persons;
            }
        }
        return round($lower/$total, 2);
    }

}